﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using AutoWelding.DAL;
using AutoWelding.Models;

namespace AutoWelding.BLL
{
    public class UserBLL
    {
        UserDAL userDAL = new UserDAL();

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="userStatus"></param>
        /// <param name="username"></param>
        /// <param name="isDeleted"></param>
        /// <returns></returns>
        public List<UserModel> GetUsers(int userStatus, string username, bool isDeleted)
        {
            // 加载用户信息
            int isDeletedCode = isDeleted ? 0 : 1;
            return userDAL.GetUsers(userStatus, username, isDeletedCode);

        }
        /// <summary>
        /// 逻辑删除用户
        /// </summary>
        /// <param name="selectedUserIds"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool LogicDeleteUser(List<int> selectedUserIds)
        {
            if(userDAL.LogicDeleteUser(selectedUserIds) > 0) 
            { 
                return true;
            }
            else
            {
                return false;
            }
        }

        // 更新用户信息
        public bool UpdateUser(UserModel user)
        {
            if(userDAL.UpdateUser(user) > 0)
            {
                Console.WriteLine("更新成功");
                return true;
            }else
            {
                Console.WriteLine("更新失败");
                return false;
                
            }
        }

        //根据id 获取用户信息
        public UserModel GetUserById(int userId)
        {
            return userDAL.GetUserById(userId);
        }
        ///<summary>
        /// 删除用户
        /// </summary>
        public bool DeleteUser(int userId)
        {
            if (userDAL.DeleteUser(userId) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="user"></param>
        /// <exception cref="NotImplementedException"></exception>
        public int InserUser(UserModel user)
        {
            user.UserPassword = "123456";
            user.CreateTime = DateTime.Now;

            return userDAL.InsertUser(user);
        }

        /// <summary>
        /// 验证用户是否已经存在
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public bool CheckUserIsExist(string username)
        {
            return userDAL.CheckUserIsExist(username) > 0;
        }

        /// <summary>
        /// 判断用户名是否已被其他用户使用（排除自己）
        /// </summary>
        public bool IsUsernameExists(string username, int excludeUserId)
        {
            return userDAL.IsUsernameExists(username, excludeUserId);
        }
    }
}
